package com.seari.tunnel.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.seari.tunnel.entity.MaintenancePlan;
import com.seari.tunnel.entity.MaintenancePlanPage;
import com.seari.tunnel.entity.MaintenanceRecord;
import com.seari.tunnel.entity.MaintenanceRecordCost;
import com.seari.tunnel.entity.MaintenanceRecordPage;
import com.seari.tunnel.entity.OperationDevice;
import com.seari.tunnel.entity.OperationDevicePage;
import com.seari.tunnel.entity.OperationDeviceParam;
import com.seari.tunnel.entity.OperationDeviceType;
import com.seari.tunnel.entity.OperationSpare;
import com.seari.tunnel.entity.RepairApply;
import com.seari.tunnel.entity.RepairApplyCost;
import com.seari.tunnel.entity.RepairApplyPage;
import com.seari.tunnel.service.IMaintenanceRepairService;
import com.seari.tunnel.service.IOperatMaintenanceService;

@Controller
public class MaintenanceRepairController {
	
	private final String SBWX_PAGE="/sbwx";
	private final String RCBY_PAGE="/rcby";
	private final String SBGL_PAGE="/sbgl";
	
	private final String BYJH_PAGE="/byjh";
	private final String ZXJH_PAGE="/zxjh";
	private final String BYJL_PAGE="/byjl";
	
	private final String WXSQ_PAGE="/wxsq";
	private final String ZXWX_PAGE="/zxwx";
	private final String WXJL_PAGE="/wxjl";
	
	private final String SAVE_PATH = "C:/save/";
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat nsdf = new SimpleDateFormat("yyyMMddHHmmss");
	
	@Autowired
	IMaintenanceRepairService maintenanceRepairService;
	@Autowired
	IOperatMaintenanceService operatMaintenanceService;

//页面部分
	@RequestMapping(value="/sbwx")
	public ModelAndView sbwx(){
		
		return new ModelAndView(SBWX_PAGE);
	}
	
	@RequestMapping(value="/rcby")
	public ModelAndView rcby(){
		
		return new ModelAndView(RCBY_PAGE);
	}
	
	@RequestMapping(value="/sbgl")
	public ModelAndView sbgl(){
		
		return new ModelAndView(SBGL_PAGE);
	}
	
	
	@RequestMapping(value="/byjh")
	public ModelAndView byjh(){
		
		return new ModelAndView(BYJH_PAGE);
	}
	@RequestMapping(value="/zxjh")
	public ModelAndView zxjh(){
		
		return new ModelAndView(ZXJH_PAGE);
	}
	@RequestMapping(value="/byjl")
	public ModelAndView byjl(){
		
		return new ModelAndView(BYJL_PAGE);
	}
	
	@RequestMapping(value="/wxsq")
	public ModelAndView wxsq(){
		
		return new ModelAndView(WXSQ_PAGE);
	}
	@RequestMapping(value="/zxwx")
	public ModelAndView zxwx(){
		
		return new ModelAndView(ZXWX_PAGE);
	}
	@RequestMapping(value="/wxjl")
	public ModelAndView wxjl(){
		
		return new ModelAndView(WXJL_PAGE);
	}
	
	@RequestMapping(value="/loadOperationDevice")
	@ResponseBody
	public Object getOperationDevice(@RequestParam("rows")int rows,@RequestParam("page")int page,@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString){
		OperationDevicePage pages = new OperationDevicePage();
		List<OperationDevice> data= maintenanceRepairService.findOperationDevice();
		int rowCount = 0;
		if(data.size()%rows==0){
			rowCount = data.size()/rows;
		}else{
			rowCount = data.size()/rows+1;
		}
		List<OperationDevice> cdata = new ArrayList<OperationDevice>();
		for(int i=0;i<rows;i++){
			if(i+rows*(page-1)<data.size()){
			cdata.add(data.get(i+rows*(page-1)));
			}
		}
		pages.setPage(page);
		pages.setRecords(data.size());
		pages.setTotal(rowCount);
		pages.setRows(cdata);
		
		if(search==true){
			List<OperationDevice> searchdata=new ArrayList<OperationDevice>();
			try {
				searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String str[]=searchString.split("&");
			for(int i=0;i<str.length;i++){
				searchdata.clear();
				for(OperationDevice sa:data){
					if(sa.getDeviceCode().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getDeviceModel().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getDeviceName().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getDeviceType().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getLocation().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getProviderName().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getState().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getUseDept().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getUseTime().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}
				}
				data=new ArrayList<OperationDevice>(searchdata);
			}
			System.out.println(searchdata.size());
			return searchdata;
		}
		return pages;
	}
	
	@RequestMapping(value="/operationDeviceEdit")
	@ResponseBody
	public ModelAndView operationDeviceEdit(@ModelAttribute OperationDeviceParam operationDeviceParam) throws Throwable{
	    if("add".equals(operationDeviceParam.getOper())){
	        OperationDevice operationDevice = new OperationDevice();
	        operationDevice.setDeviceCode(operationDeviceParam.getDeviceCode());
	        operationDevice.setDeviceName(operationDeviceParam.getDeviceName());
	        operationDevice.setDeviceModel(operationDeviceParam.getDeviceModel());
	        operationDevice.setDeviceType(operationDeviceParam.getDeviceType());
	        operationDevice.setUseTime(operationDeviceParam.getUseTime());
	        operationDevice.setProviderName(operationDeviceParam.getProviderName());
	        operationDevice.setUseDept(operationDeviceParam.getUseDept());
	        operationDevice.setLocation(operationDeviceParam.getLocation());
	        operationDevice.setState(operationDeviceParam.getState());
	        maintenanceRepairService.addOperationDevice(operationDevice);
		    
		}
		else if("edit".equals(operationDeviceParam.getOper())){
			OperationDevice operationDevice = new OperationDevice();
			operationDevice.setId(Integer.parseInt(operationDeviceParam.getId()));
	        operationDevice.setDeviceCode(operationDeviceParam.getDeviceCode());
	        operationDevice.setDeviceName(operationDeviceParam.getDeviceName());
	        operationDevice.setDeviceModel(operationDeviceParam.getDeviceModel());
	        operationDevice.setDeviceType(operationDeviceParam.getDeviceType());
	        operationDevice.setUseTime(operationDeviceParam.getUseTime());
	        operationDevice.setProviderName(operationDeviceParam.getProviderName());
	        operationDevice.setUseDept(operationDeviceParam.getUseDept());
	        operationDevice.setLocation(operationDeviceParam.getLocation());
	        operationDevice.setState(operationDeviceParam.getState());
	        maintenanceRepairService.updateOperationDevice(operationDevice);
		}
		else{
			int id=0;
			if(operationDeviceParam.getId().contains(",")){
				id=Integer.parseInt(operationDeviceParam.getId().replace(',', ' ').trim());
			}else{
				id=Integer.parseInt(operationDeviceParam.getId());
			}
			maintenanceRepairService.deleteOperationDevice(id);
		}
		return new ModelAndView(SBGL_PAGE);
	}
	
	@RequestMapping(value="/checkOperationType")
	@ResponseBody
	public Object checkOperationType(){
		List<OperationDeviceType> data= maintenanceRepairService.findOperationDeviceType();
		return data;
	}
	
	@RequestMapping(value="/checkOperationDevice")
	@ResponseBody
	public Object checkOperationDevice(){
		List<OperationDevice> data= maintenanceRepairService.findOperationDevice();
		return data;
	}
	
	@RequestMapping(value="/checkMaintenancePlan")
	@ResponseBody
	public Object checkMaintenancePlan(){
		List<MaintenancePlan> data= maintenanceRepairService.findUnfinishedMaintenancePlan();
		return data;
	}
	
	//养护计划
		@RequestMapping(value="/loadMaintenancePlan")
		@ResponseBody
		public Object getMaintenancePlan(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			MaintenancePlanPage pages = new MaintenancePlanPage();
			List<MaintenancePlan> data=maintenanceRepairService.findMaintenancePlan();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<MaintenancePlan> cdata = new ArrayList<MaintenancePlan>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<MaintenancePlan> searchdata=new ArrayList<MaintenancePlan>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(MaintenancePlan sa:data){
						if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceModel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceType().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getState().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLastTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getNextTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<MaintenancePlan>(searchdata);
				}
				
				return searchdata;
			}
			return pages;
		}
		@RequestMapping(value="/operMaintenancePlan")
		public void operMaintenancePlan(@ModelAttribute MaintenancePlan maint,@RequestParam("oper") String oper) throws Throwable{
			
			if("add".equals(oper.toString())){
				MaintenancePlan maintenancePlan=new MaintenancePlan();
				maintenancePlan.setOd_id(maint.getOd_id());
				maintenancePlan.setState("1");
				maintenancePlan.setLastTime(maint.getLastTime());
				maintenancePlan.setNextTime(maint.getNextTime());
				maintenancePlan.setInterval(maint.getInterval());
				maintenancePlan.setAround(maint.getAround());
				maintenancePlan.setLevel(maint.getLevel());
				maintenancePlan.setPoint(maint.getPoint());
				maintenancePlan.setStandard(maint.getStandard());
				maintenancePlan.setPerson(maint.getPerson());
				maintenancePlan.setDetail(maint.getDetail());
				maintenanceRepairService.addMaintenancePlan(maintenancePlan);
				
			}else if("edit".equals(oper.toString())){
				
				MaintenancePlan maintenancePlan=new MaintenancePlan();
				maintenancePlan.setId(maint.getId());
				maintenancePlan.setOd_id(maint.getOd_id());
				maintenancePlan.setState("1");
				maintenancePlan.setLastTime(maint.getLastTime());
				maintenancePlan.setNextTime(maint.getNextTime());
				maintenancePlan.setInterval(maint.getInterval());
				maintenancePlan.setAround(maint.getAround());
				maintenancePlan.setLevel(maint.getLevel());
				maintenancePlan.setPoint(maint.getPoint());
				maintenancePlan.setStandard(maint.getStandard());
				maintenancePlan.setPerson(maint.getPerson());
				maintenancePlan.setDetail(maint.getDetail());
				maintenanceRepairService.updateMaintenancePlan(maintenancePlan);
				
			}else if("del".equals(oper.toString())){
				
				MaintenancePlan maintenancePlan=new MaintenancePlan();
				String id;
				if(maint.getId().contains(",")){
				  	id = maint.getId().replace(',', ' ').trim();
				}else{
					id = maint.getId();
				}
				maintenancePlan.setId(id);
				maintenanceRepairService.deleteMaintenancePlan(maintenancePlan);
			}
			
		}
		
		
	//养护记录
		@RequestMapping(value="/loadMaintenanceRecord")
		@ResponseBody
		public Object getMaintenanceRecord(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			MaintenanceRecordPage pages = new MaintenanceRecordPage();
			List<MaintenanceRecord> data=maintenanceRepairService.findMaintenanceRecord();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<MaintenanceRecord> cdata = new ArrayList<MaintenanceRecord>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<MaintenanceRecord> searchdata=new ArrayList<MaintenanceRecord>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(MaintenanceRecord sa:data){
						if(sa.getMaintenanceNo().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStartTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getEndTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if((sa.getMp_id()+"").indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<MaintenanceRecord>(searchdata);
				}
				return searchdata;
			}
			return pages;
		}
		@RequestMapping(value="/operMaintenanceRecord")
		public void operMaintenanceRecord(@ModelAttribute MaintenanceRecord maint,@RequestParam("oper") String oper){
			
		  if("edit".equals(oper.toString())){
				
				MaintenanceRecord maintenanceRecord=new MaintenanceRecord();
				maintenanceRecord.setId(maint.getId());
				maintenanceRecord.setState(maint.getState());
				maintenanceRecord.setEndTime(maint.getEndTime());
				maintenanceRecord.setFee(maint.getFee());
				maintenanceRecord.setDetail(maint.getDetail());
				maintenanceRepairService.updateMaintenanceRecord(maintenanceRecord);
			}else if("del".equals(oper.toString())){
				MaintenanceRecord maintenanceRecord=new MaintenanceRecord();
				String id;
				if(maint.getId().contains(",")){
				  	id = maint.getId().replace(',', ' ').trim();
				}else{
					id = maint.getId();
				}
				maintenanceRecord.setId(id);
				maintenanceRepairService.delMaintenanceRecord(maintenanceRecord);
			}
		}
		
		@RequestMapping(value="/loadfinishMaintenanceRecord")
		@ResponseBody
		public Object getfinishMaintenanceRecord(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			MaintenanceRecordPage pages = new MaintenanceRecordPage();
			List<MaintenanceRecord> data=maintenanceRepairService.findFinishMaintenanceRecord();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<MaintenanceRecord> cdata = new ArrayList<MaintenanceRecord>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<MaintenanceRecord> searchdata=new ArrayList<MaintenanceRecord>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(MaintenanceRecord sa:data){
						if(sa.getMaintenanceNo().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStartTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getEndTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if((sa.getMp_id()+"").indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<MaintenanceRecord>(searchdata);
				}
				return searchdata;
			}
			return pages;
		}
		
	//维修申请
		@RequestMapping(value="/loadRepairApply")
		@ResponseBody
		public Object loadRepairApply(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			RepairApplyPage pages = new RepairApplyPage();
			List<RepairApply> data=maintenanceRepairService.findRepairApply();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<RepairApply> cdata = new ArrayList<RepairApply>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<RepairApply> searchdata=new ArrayList<RepairApply>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(RepairApply sa:data){
						if(sa.getRepairNo().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getState().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getApplyMan().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getCreateTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStartTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getEndTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStopTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<RepairApply>(searchdata);
				}
				return searchdata;
			}
			return pages;
		}
		
		@RequestMapping(value="/operRepairApply")
		public void operRepairApply(@ModelAttribute RepairApply repai,@RequestParam("oper") String oper){
           if("del".equals(oper.toString())){
				
				RepairApply repairApply=new RepairApply();
				String id;
				if(repai.getId().contains(",")){
				  	id = repai.getId().replace(',', ' ').trim();
				}else{
					id = repai.getId();
				}
				repairApply.setId(id);
				maintenanceRepairService.deleteRepairApply(repairApply);
			}else if("edit".equals(oper.toString())){
				
				RepairApply repairApply=new RepairApply();
				repairApply.setState(repai.getState());
				repairApply.setId(repai.getId());
				repairApply.setStartTime(repai.getStartTime());
				try {
					repairApply.setRepairMan(new String(repai.getRepairMan().getBytes("ISO8859-1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				
				maintenanceRepairService.updateRepairApply(repairApply);
			}
		}
		
		@RequestMapping(value="/addRepairApply")
		public void addRepairApply(@RequestParam("operationDevice") int operationDevice,@RequestParam("applyMan") String applyMan,@RequestParam("level") String level,@RequestParam("detail") String detail,@RequestParam("fileField") MultipartFile fileField){
			String savePath =  SAVE_PATH+"repairApply";
	        File file = new File(savePath);
	        if (!file.exists() && !file.isDirectory()) {
	            //创建目录
	            file.mkdir();
	       }
	        String fileType = fileField.getContentType();
	        String filename = fileField.getOriginalFilename();
	        if("application/vnd.openxmlformats-officedocument.wordprocessingml.document".equals(fileType) || "application/msword".equals(fileType) || "application/pdf".equals(fileType) || "image/jpeg".equals(fileType)||"image/gif".equals(fileType)){
	        	filename = filename.substring(filename.lastIndexOf("\\")+1);
	        	try {
					InputStream in = fileField.getInputStream();
					FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
					//创建一个缓冲区
				    byte buffer[] = new byte[1024];
				    //判断输入流中的数据是否已经读完的标识
					int len = 0;
					 while((len=in.read(buffer))>0){
						//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
						out.write(buffer, 0, len);
						 }
						//关闭输入流
						in.close();
						//关闭输出流
						out.close();
				} catch (IOException e) {
					
					e.printStackTrace();
				}
	        	RepairApply repairApply=new RepairApply();
	        	try {
					repairApply.setOd_id(operationDevice);
					Date d = new Date();
					String date = sdf.format(d);
					repairApply.setRepairNo("WX"+nsdf.format(d));
					repairApply.setCreateTime(date);
					repairApply.setApplyMan(new String(applyMan.getBytes("ISO8859-1"),"UTF-8"));
					repairApply.setState("1");
					repairApply.setLevel(level);
					repairApply.setDetail(new String(detail.getBytes("ISO8859-1"),"UTF-8"));
					repairApply.setWrongFile(filename);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				maintenanceRepairService.addRepairApply(repairApply);
	        }
		
		}
		
		@RequestMapping(value="/repairApplyDownload")
		public void repairApplyDownload(@RequestParam("file") String file,HttpServletRequest req, HttpServletResponse res){
			InputStream is = null;
			OutputStream os = null;
			try {
				res.reset();
				String fileName = new String(file.getBytes("ISO8859-1"),"UTF-8");
				String fName = "";
				if(req.getHeader("User-Agent").toLowerCase().indexOf("firefox")>0){
					fName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
	            }else if (req.getHeader("User-Agent").toUpperCase().indexOf("MSIE")>0){
	            	fName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
	            }else{
	            	fName = URLEncoder.encode(fileName, "UTF-8");
	            }
			res.setCharacterEncoding("utf-8");
			if(fileName.contains("doc") || fileName.contains("docx")){
				res.setContentType("application/msword");
			}
	        if(fileName.contains("pdf")){
	        	res.setContentType("application/pdf");
			}
	        if(fileName.contains("jpg")){
	        	res.setContentType("image/jpeg");
	        }
			res.setHeader("Content-Disposition", "attachment;fileName=\"" + fName + "\"");

	            String path = SAVE_PATH+"repairApply";
	            File filePath = new File(path);
	            is = new FileInputStream(new File(filePath
	                    + File.separator + fileName));
	 
	            os = res.getOutputStream();
	            byte[] mybyte = new byte[8192];
	            int len = 0;
	            while ((len = is.read(mybyte)) != -1) {
	                os.write(mybyte, 0, len);
	            }

	            os.flush();
	             // 这里主要关闭。
	            os.close();
	            is.close();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		}
		
		
	//维修记录
		@RequestMapping(value="/loadRepairRecord")
		@ResponseBody
		public Object loadRepairRecord(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			RepairApplyPage pages = new RepairApplyPage();
			List<RepairApply> data=maintenanceRepairService.findRepairRecord();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<RepairApply> cdata = new ArrayList<RepairApply>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<RepairApply> searchdata =new ArrayList<RepairApply>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(RepairApply sa:data){
						if(sa.getRepairNo().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getState().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getApplyMan().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getCreateTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStartTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getEndTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStopTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<RepairApply>(searchdata);
				}
				return searchdata;
			}
			return pages;
		}
		
		@RequestMapping(value="/loadFinishRepairRecord")
		@ResponseBody
		public Object loadFinishRepairRecord(@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString,@RequestParam("rows")int rows,@RequestParam("page")int page){
			RepairApplyPage pages = new RepairApplyPage();
			List<RepairApply> data=maintenanceRepairService.findFinishRepairRecord();
			int rowCount = 0;
			if(data.size()%rows==0){
				rowCount = data.size()/rows;
			}else{
				rowCount = data.size()/rows+1;
			}
			List<RepairApply> cdata = new ArrayList<RepairApply>();
			for(int i=0;i<rows;i++){
				if(i+rows*(page-1)<data.size()){
				cdata.add(data.get(i+rows*(page-1)));
				}
			}
			pages.setPage(page);
			pages.setRecords(data.size());
			pages.setTotal(rowCount);
			pages.setRows(cdata);
			
			if(search==true){
				List<RepairApply> searchdata =new ArrayList<RepairApply>();
				try {
					searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String str[]=searchString.split("&");
				for(int i=0;i<str.length;i++){
					searchdata.clear();
					for(RepairApply sa:data){
						if(sa.getRepairNo().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getState().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceCode().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getDeviceName().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getApplyMan().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getCreateTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStartTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getEndTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getStopTime().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}else if(sa.getLevel().indexOf(str[i])!=-1){
							searchdata.add(sa);
							continue;
						}
					}
					data=new ArrayList<RepairApply>(searchdata);
				}
				return searchdata;
			}
			return pages;
		}
		
		@RequestMapping(value="/operRepairRecord")
		public void operRepairRecord(@ModelAttribute RepairApply repai,@RequestParam("oper") String oper)throws Throwable{
			if("edit".equals(oper.toString())){
				RepairApply repairRecord=new RepairApply();
				repairRecord.setId(repai.getId());
				repairRecord.setState(repai.getState());
				repairRecord.setEndTime(sdf.format(new Date()));
				repairRecord.setStopTime(repai.getStopTime());
				repairRecord.setResponsibleMan(repai.getResponsibleMan());
				repairRecord.setFee(repai.getFee());
				repairRecord.setWorkDetail(repai.getWorkDetail());
				maintenanceRepairService.updateRepairRecord(repairRecord);
			}else if("del".equals(oper.toString())){
				
				RepairApply repairRecord=new RepairApply();
				String id;
				if(repai.getId().contains(",")){
				  	id = repai.getId().replace(',', ' ').trim();
				}else{
					id = repai.getId();
				}
				repairRecord.setId(id);
				maintenanceRepairService.deleteRepairApply(repairRecord);
			}
		}
		
		@RequestMapping(value = "/getOperationSpare")
		@ResponseBody
		public Object getOperationSpare() {
			List<OperationSpare> data = operatMaintenanceService.findOperationSpare();
			return data;
		}
		
		@RequestMapping(value = "/getSpareCount")
		@ResponseBody
		public Object getSpareCount(@RequestParam("id") int id) {
			OperationSpare os = maintenanceRepairService.findOperationSpareById(id);
			return os;
		}
		
		@RequestMapping(value = "/doMaintenancePlan")
		public void doMaintenancePlan(@RequestParam("id") int id) throws Throwable{
			MaintenanceRecord maintenanceRecord=new MaintenanceRecord();
			Date date = new Date();
			maintenanceRecord.setMaintenanceNo("BY"+nsdf.format(date));
			maintenanceRecord.setState("1");
			maintenanceRecord.setStartTime(sdf.format(date));
			maintenanceRecord.setMp_id(id);
			maintenanceRepairService.addMaintenanceRecord(maintenanceRecord);
		}
		
		@RequestMapping(value = "/addOperationSpareCost")
		public void addOperationSpareCost(@RequestParam("id") int id,@RequestParam("os_id") int os_id,@RequestParam("costNum") String costNum) throws Throwable{
			maintenanceRepairService.addOperationSpareCost(id, os_id, costNum);
		}
		
		@RequestMapping(value = "/addRepairApplyCost")
		public void addRepairApplyCost(@RequestParam("id") int id,@RequestParam("os_id") int os_id,@RequestParam("costNum") String costNum) throws Throwable{
			maintenanceRepairService.addRepairApplyCost(id, os_id, costNum);
		}
		
		@RequestMapping(value="/findOSCostById")
		@ResponseBody
		public Object findOSCostById(@RequestParam("id") int id){
			List<MaintenanceRecordCost> data= maintenanceRepairService.findOSCostById(id);
			return data;
		}
		
		@RequestMapping(value="/findRACostById")
		@ResponseBody
		public Object findRACostById(@RequestParam("id") int id){
			List<RepairApplyCost> data= maintenanceRepairService.findRACostById(id);
			return data;
		}
		
		@RequestMapping(value="/loadInfo")
		@ResponseBody
		public Object loadInfo(@RequestParam("DeviceCode")String DeviceCode){
			List<MaintenancePlan> data= maintenanceRepairService.findMaintenancePlanByDeviceCode(DeviceCode);
			return data;
		}
}
